//
//  ProjectDetailController.swift
//  JianTingSwift
//
//  Created by zhibao on 2024/11/21.
//

import UIKit

class ProjectDetailController: SecSuperController {

    var dic = [String:String]()
    var scrollview = UIScrollView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.gk_navTitle = "商品详情"
        QMUITips.showLoading(in: self.view)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            QMUITips.hideAllTips()
            self.hideNavLine()
            self.initView()
        }
    }
    

    private func initView(){
        
        let bottomView = UIView()
        bottomView.backgroundColor = .white
        self.view.addSubview(bottomView)
        bottomView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self.view)
            make.height.equalTo(pixw(p: 100))
        }
        
        if bottomView != nil {
            let line = UIView()
            line.backgroundColor = MYColor.color(withHexString: "e5e5e5")
            bottomView.addSubview(line)
            line.snp.makeConstraints { make in
                make.left.right.top.equalTo(bottomView)
                make.height.equalTo(pixw(p: 1))
            }
            
            let kefuBtn = UIButton(type: .custom)
            kefuBtn.setImage(UIImage(named: "detail_kefu"), for: .normal)
            kefuBtn.setTitleColor(MYColor.color(withHexString: "666666"), for: .normal)
            kefuBtn.titleLabel?.font = MY_SCALE_MediumFONT(14)
            kefuBtn.setTitle("  联系客服  ", for: .normal)
            bottomView.addSubview(kefuBtn)
            kefuBtn.snp.makeConstraints { make in
                make.left.equalTo(pixw(p: 20))
                make.top.bottom.equalTo(bottomView)
                make.width.equalTo(pixw(p: 100))
            }
            kefuBtn.addGestureRecognizer(UITapGestureRecognizer(actionBlock: { sender in
                let phoneUrl = "telprompt:///010-8398743"
                UIApplication.shared.open(URL(string: phoneUrl)!, options: [:], completionHandler: { (success) in
                       if success {
                           print("URL opened successfully")
                       } else {
                           print("Failed to open URL")
                       }
                   })
            }))
            
            
            let zulinBtn = UIButton(type: .custom)
            zulinBtn.setTitleColor(MYColor.color(withHexString: "ffffff"), for: .normal)
            zulinBtn.titleLabel?.font = MY_SCALE_MediumFONT(16)
            zulinBtn.backgroundColor = MYColor.color(withHexString: "#FC4A17")
            zulinBtn.layer.cornerRadius = pixw(p: 22)
            zulinBtn.setTitle("立即预约", for: .normal)
            bottomView.addSubview(zulinBtn)
            zulinBtn.snp.makeConstraints { make in
                make.right.equalTo(pixw(p: -18))
                make.centerY.equalTo(bottomView)
                make.width.equalTo(pixw(p: 133))
                make.height.equalTo(pixw(p: 44))
            }
            zulinBtn.addGestureRecognizer(UITapGestureRecognizer(actionBlock: { sender in
               let controller = SureDingDanController()
                controller.dic = self.dic
                self.navigationController?.pushViewController(controller, animated: true)
            }))
            
        }
        
        
        self.view.addSubview(scrollview)
        scrollview.backgroundColor = .white
        scrollview.snp.makeConstraints { make in
            make.left.right.equalTo(self.view)
            make.top.equalTo(navigationFullHeight())
            make.bottom.equalTo(bottomView.snp.top)
        }
        let shangpinImg = UIImage(named: dic["img"] ?? "")
        let shangpinImgView = UIImageView(image: shangpinImg)
        scrollview.addSubview(shangpinImgView)
        shangpinImgView.snp.makeConstraints { make in
            make.left.top.equalTo(scrollview)
            make.width.equalTo(SCREEN_WIDTH)
            make.height.equalTo(SCREEN_WIDTH * shangpinImg!.size.height / shangpinImg!.size.width)
        }
        
        let zhimaImg = UIImage(named: "detail_zhima")
        let zhimaImgView = UIImageView(image: zhimaImg)
        scrollview.addSubview(zhimaImgView)
        zhimaImgView.snp.makeConstraints { make in
            make.left.bottom.width.equalTo(shangpinImgView)
            make.height.equalTo(SCREEN_WIDTH * zhimaImg!.size.height / zhimaImg!.size.width)
        }
        
        if dic["new"] != "" {
            let newImgView = UIImageView(image: UIImage(named: dic["new"] ?? ""))
            scrollview.addSubview(newImgView)
            newImgView.snp.makeConstraints { make in
                make.left.top.equalTo(shangpinImgView)
                make.width.equalTo(pixw(p: 50))
                make.height.equalTo(pixw(p: 18))
            }
        }
        
        let nameLabel = UILabel()
        nameLabel.text = dic["name"]
        nameLabel.font = MY_SCALE_MediumFONT(15)
        nameLabel.textColor = MYBlock
        nameLabel.numberOfLines = 0
        nameLabel.lineBreakMode = .byWordWrapping
        scrollview.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(pixw(p: 20))
            make.right.equalTo(self.view).offset(pixw(p: -20));
            make.top.equalTo(shangpinImgView.snp.bottom).offset(pixw(p:10));
        }
        
        let qianPriceLabel = UILabel()
        qianPriceLabel.text = "¥"
        qianPriceLabel.font = MY_SCALE_LightFONT(12)
        qianPriceLabel.textColor = MYRed
        scrollview.addSubview(qianPriceLabel)
        qianPriceLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel);
            make.top.equalTo(nameLabel.snp.bottom).offset(pixw(p: 10));
        }
        
        let priceLabel = UILabel()
        priceLabel.text = dic["price"]
        priceLabel.font = MY_SCALE_BoldFont(20)
        priceLabel.textColor = MYRed
        scrollview.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { make in
            make.left.equalTo(qianPriceLabel.snp.right).offset(0);
            make.bottom.equalTo(qianPriceLabel);
        }
        
        let cpriceLabel = UILabel()
        cpriceLabel.text = "/天"
        cpriceLabel.font = MY_SCALE_LightFONT(12)
        cpriceLabel.textColor = MYRed
        scrollview.addSubview(cpriceLabel)
        cpriceLabel.snp.makeConstraints { make in
            make.left.equalTo(priceLabel.snp.right).offset(0);
            make.bottom.equalTo(qianPriceLabel);
        }
        
        let baoYouLabel = UILabel()
        baoYouLabel.text = "   vip独享:\(String(describing: dic["vip"] ?? ""))   "
        baoYouLabel.font = MY_SCALE_RegularFONT(11)
        baoYouLabel.layer.cornerRadius = pixw(p: 3)
        baoYouLabel.layer.masksToBounds = true
        baoYouLabel.backgroundColor = MYColor.color(withHexString: "#E95A27", alpha: 0.1)
        baoYouLabel.textColor = MYRed
        scrollview.addSubview(baoYouLabel)
        baoYouLabel.snp.makeConstraints { make in
            make.left.equalTo(cpriceLabel.snp.right).offset(pixw(p: 2));
            make.centerY.equalTo(cpriceLabel);
            make.height.equalTo(pixw(p: 18))
        }
        
        let dayLabel = UILabel()
        dayLabel.text = "\(String(describing: dic["day"] ?? ""))天起租"
        dayLabel.font = MY_SCALE_RegularFONT(12)
        dayLabel.textColor = MYColor.color(withHexString: "#999999")
        scrollview.addSubview(dayLabel)
        dayLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel);
            make.top.equalTo(cpriceLabel.snp.bottom).offset(pixw(p: 10));
        }
        
        let yajinLabel = UILabel()
        yajinLabel.text = "押金:\(String(describing: dic["yajin"] ?? ""))元"
        yajinLabel.font = MY_SCALE_RegularFONT(12)
        yajinLabel.textColor = MYRed
        scrollview.addSubview(yajinLabel)
        yajinLabel.snp.makeConstraints { make in
            make.right.equalTo(nameLabel);
            make.centerY.equalTo(cpriceLabel);
        }
        
        let shifouLabel = UILabel()
        shifouLabel.text = "是否免押，以芝麻信用为准"
        shifouLabel.font = MY_SCALE_RegularFONT(10)
        shifouLabel.textColor = MYColor.color(withHexString: "999999")
        scrollview.addSubview(shifouLabel)
        shifouLabel.snp.makeConstraints { make in
            make.right.equalTo(nameLabel);
            make.centerY.equalTo(dayLabel);
        }
        
        let line = UIView()
        line.backgroundColor = MYColor.color(withHexString: "f5f5f5")
        scrollview.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.equalTo(self.view)
            make.top.equalTo(dayLabel.snp.bottom).offset(pixw(p: 16))
            make.height.equalTo(pixw(p: 10))
        }
        
        let liuchengLabel = UILabel()
        liuchengLabel.text = "租赁流程"
        liuchengLabel.font = MY_SCALE_MediumFONT(15)
        liuchengLabel.textColor = MYColor.color(withHexString: "3d3d3d")
        scrollview.addSubview(liuchengLabel)
        liuchengLabel.snp.makeConstraints { make in
            make.left.equalTo(pixw(p: 16))
            make.top.equalTo(line.snp.bottom).offset(pixw(p: 10))
        }
        
        let liuchengImg = UIImage(named: "detail_liucheng")
        let liuchengImgView = UIImageView(image: liuchengImg)
        scrollview.addSubview(liuchengImgView)
        liuchengImgView.snp.makeConstraints { make in
            make.left.equalTo(liuchengLabel)
            make.top.equalTo(liuchengLabel.snp.bottom).offset(pixw(p: 10))
            make.width.equalTo(SCREEN_WIDTH - pixw(p: 32))
            make.height.equalTo(pixw(p: 63))
        }
        
        let shiyiLabel = UILabel()
        shiyiLabel.text = "租赁事宜"
        shiyiLabel.font = MY_SCALE_MediumFONT(15)
        shiyiLabel.textColor = MYColor.color(withHexString: "3d3d3d")
        scrollview.addSubview(shiyiLabel)
        shiyiLabel.snp.makeConstraints { make in
            make.left.equalTo(pixw(p: 16))
            make.top.equalTo(liuchengImgView.snp.bottom).offset(pixw(p: 18))
        }
        
        let shiyiImg = UIImage(named: "detail_guize")
        let shiyiImgView = UIImageView(image: shiyiImg)
        scrollview.addSubview(shiyiImgView)
        shiyiImgView.snp.makeConstraints { make in
            make.left.equalTo(shiyiLabel)
            make.top.equalTo(shiyiLabel.snp.bottom).offset(pixw(p: 10))
            make.width.equalTo(SCREEN_WIDTH - pixw(p: 32))
            make.height.equalTo(pixw(p: 133))
            make.bottom.equalTo(pixw(p: -30))
        }
    }

}
